package com.apestech.api.tables;

import com.apestech.api.tables.modul.SeqField;
import com.apestech.framework.db.SqlSession;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.sql.callback.SaveTableCallback;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.xml.table.modul.Tables;
import java.util.ArrayList;

/**
 * @author hup
 * @create 2019-01-29-11:01
 */
public class TableExec extends StringUtil {
    private SqlSession session;
    private Tables tables;
    private SeqField seqField;

    public TableExec(SqlSession session, Tables tables){
        this.session=session;
        this.tables=tables;
    }

    public void setSeqField(SeqField seqField) {
        this.seqField = seqField;
    }

    public void createTable(boolean bdrop) throws Exception {
        if(bdrop){
            session.getForDDL().dropTables(tables);
        }
        if(session.tableExists(tables.getMasterTable())){
            System.out.println("表["+tables.getMasterTable()+"]已经存在，如果确认要重建，请人工删除之后再执行建表操作");
            return;
        }
        session.getForDDL().createTable(tables);
        if(seqField!=null){
            SequenceManage sequenceManage=new SequenceManage(session);
            sequenceManage.createSeq(seqField, true);
        }
    }

    public void createTableSql() throws Exception {
        //session.getForDDL().dropTablesSql(tables);
        if(session.tableExists(tables.getMasterTable())){
            System.out.println("该表已经存在，如果确认要重建，请人工删除之后再执行建表操作");
        }
        session.getForDDL().createTableSql(tables);
        if(seqField!=null){
            SequenceManage sequenceManage=new SequenceManage(session);
            sequenceManage.createSeq(seqField, false);
        }
    }

    public void deleteTable(Object id) throws Exception {
        JSONObject m=tables.getIdMap(id);
        String tabName=tables.getMasterTable();
        m.putAll(tables.getSelectMap());
        JSONArray list=session.getForJSON().table(tabName, m);
        if(list.size()==0){
            throw new Exception("该数据已被删除，不允许继续操作！");
        }
        m.putAll(tables.getTimeMap());
        String timefield=tables.getTimeField();
        m.put(timefield, (list.get(0)).get(timefield));
        deleteTable(m);
    }

    public void emptyTable() throws Exception {
        deleteTable(new JSONObject());
    }

    public void deleteTable(JSONObject input) throws Exception {
        session.getForDDL().deleteMasterTable(tables, input);
    }

    public JSONArray queryTable(Object id) throws Exception {
        JSONObject m=tables.getIdMap(id);
        return session.getForDDL().queryMasterTable(tables, m);
    }

    public void getDataXml() throws Exception {
        session.getForDDL().getDataXml(tables);
    }

    public Object saveMasterTable() throws Exception {
        return session.getForDDL().saveMasterTable(tables);
    }

    public void execTable(SaveTableCallback saveTable, JSONObject input) throws Exception {
        execTable(tables.getMaster().getTable().getName(), input, 0, saveTable);
    }

    public void execTableAccount(Object data, SaveTableCallback saveTable) throws Exception {
        if(data==null) return;
        JSONArray dlist=null;
        if(data instanceof JSONArray) dlist= (JSONArray) data;
        else if(data instanceof JSONObject){
            dlist=new JSONArray();
            dlist.add(data);
        }
        int len=dlist.size();
        if(len==0) return;
        for(int i=0; i<len; i++){
            JSONObject m=tables.getIdMap(dlist.get(i));
            execTable(tables.getMaster().getTable().getName(), dlist.get(i), 0, saveTable);
        }
    }

    public void execTable(Object id, SaveTableCallback saveTable) throws Exception {
        JSONObject m=tables.getIdMap(id);
        JSONArray list=session.getForDDL().queryMasterTable(tables, m);
        if(list.size()==1) m=list.get(0);
        execTable(tables.getMaster().getTable().getName(), m, 0, saveTable);
    }

    private void execTable(String tabName, JSONObject input, int index, SaveTableCallback saveTable) throws Exception {
        if(input==null) return;
        if(!saveTable.before(tabName)) return;
        saveTable.write(tabName, input, index, session);
        String msg=saveTable.after(tabName, input);
        if(msg!=null) throw new Exception(msg);
        ArrayList<String> klist=input.getKeylist();
        int len=klist.size();
        for(int i=0; i<len; i++){
            String key=klist.get(i);
            if(input.get(key) instanceof JSONArray) {
                JSONArray mlist= (JSONArray) input.get(key);
                int mlen=mlist.size();
                for(int m=0; m<mlen; m++){
                    JSONObject mx=new JSONObject(input);
                    mx.putAll(mlist.get(m));
                    ArrayList<String> clist= (ArrayList<String>) tables.getMaster().getChild().get(tabName);
                    int clen=clist.size();
                    for(int c=0; c<clen; c++) mx.remove(clist.get(c));
                    execTable(key, mx, m, saveTable);
                }
            }
        }
    }

}
